/**
 * 文件名：****.java.
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：text.
 */
package com.lightkits.wms.domain.orderscanner.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.FndEventTypeBase;
import com.lightkits.common.db.entity.event.BdEvent;
import com.lightkits.common.db.entity.event.BdEventRecord;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.object.ParseUtil;
import com.lightkits.framework.soa.client.id.GetGlobalIDRequestType;
import com.lightkits.framework.soa.client.id.GetGlobalIDResponseType;
import com.lightkits.framework.soa.client.id.IDServiceClient;
import com.lightkits.wms.domain.orderscanner.CreateScaEventParams;
import com.lightkits.wms.dto.orderscanner.model.ScannerHeaderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;

/**
 * @author jing.wei@hand-china.com
 * @date 2019/6/20 18:37
 **/
@Service
public class CreateScaEventParamsImpl implements CreateScaEventParams {

    private final Logger logger = LoggerFactory.getLogger(CreateScaEventParamsImpl.class);

    private final IDServiceClient idAdapterService;

    @Autowired
    public CreateScaEventParamsImpl(IDServiceClient idAdapterService) {
        this.idAdapterService = idAdapterService;
    }

    /**
     * 创建事件
     *
     * @param params        调拨发出头请求参数
     * @param eventTypeCode 事件类型编码
     * @return BdEvent
     **/
    @Override
    public CheckResult<BdEvent> createEvent(ScannerHeaderDto params, String eventTypeCode) {
        CheckResult<BdEvent> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        GetGlobalIDRequestType idRequest = new GetGlobalIDRequestType();
        idRequest.setCount(1);
        GetGlobalIDResponseType idResponse = idAdapterService.getGlobalId(idRequest);
        Iterator<Long> sfIds = idResponse.getIds().iterator();
        BdEvent event = new BdEvent();
        try {
            FndEventTypeBase eventTypeBase = new FndEventTypeBase()
                    .selectOne(new EntityWrapper<FndEventTypeBase>()
                            .eq(FndEventTypeBase.EVENT_TYPE_CODE, eventTypeCode));

            event.setEventTypeId(eventTypeBase.getId());
            event.setEventTypeCode(eventTypeBase.getEventTypeCode());
            event.setEventRequestId(sfIds.next());
            if (!ObjectUtil.isNullOrEmpty(params.getWorkerId())) {
                event.setWorkerId(ParseUtil.tryLongWithException(params.getWorkerId()));
            }
            if (!ObjectUtil.isNullOrEmpty(params.getOrgId())) {
                event.setOrganizationId(ParseUtil.tryLongWithException(params.getOrgId()));

            }
            event.setCalendarDay(new Date());
            checkResult.setData(event);
        } catch (Exception e) {
            logger.error("createEvent.error", e);
        }
        checkResult.setData(event);
        return checkResult;
    }

    /**
     * 事件快照的创建
     *
     * @param objectType     事件对象类型
     * @param keyValue       事件关键值
     * @param objectId       事件对象ID
     * @param recordType     事件记录类型
     * @param snapShotRecord 事件快照记录，JSON格式保存
     * @return BdEventRecord
     **/
    @Override
    public CheckResult<BdEventRecord> createEventRecord(String objectType,
                                                        String keyValue,
                                                        Long objectId,
                                                        String recordType,
                                                        String snapShotRecord) {
        CheckResult<BdEventRecord> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        BdEventRecord eventRecord = new BdEventRecord();
        try {
            eventRecord.setObjectId(objectId);
            eventRecord.setObjectType(objectType);
            eventRecord.setRecordType(recordType);
            eventRecord.setKeyValue(keyValue);
            eventRecord.setSnapshotRecord(snapShotRecord);
            checkResult.setData(eventRecord);
        } catch (Exception e) {
            logger.error("createEventRecord.error", e);
        }
        return checkResult;
    }
}
